public class AccountPhoneTriggerHandler{
        static Set<Id> accphoneupdateid=new Set<Id>();
        static Set<Id> accphoneinsertid=new Set<Id>();
        
  public void AccountPhoneInsert(List<Account_Phone__c> accphonelist)    {
             Set<Id> phoneid=new Set<Id>();
             Set<Id> accid=new Set<Id>();
             
             List<Account_Phone__c> oldaccountphonelist=new List<Account_Phone__c>();
             List<Account_Phone__c> oldaccountphoneupdatelist=new List<Account_Phone__c>();
             Account_Phone__c phone=new Account_Phone__c();
          if(accphonelist!=null&&accphonelist.size()>0){
          phone=accphonelist[0];
              for(Account_Phone__c accphoneval: accphonelist){
                  phoneid.add(accphoneval.id);
                  accid.add(accphoneval.Account__c);
               }
              
             oldaccountphonelist=[select Account__c,Preferred__c,Phone_Type__c from Account_Phone__c where Preferred__c=true AND Account__c IN:accid AND Id NOT IN:phoneid];
               
           for(Account_Phone__c accphone:accphonelist){
               // To insert country code..
                    if(accphone.Country__c!=null){
                        String country=accphone.Country__c;
                        String c1=country.substring((country.indexOf('(')+1),country.indexOf(')'));
                        accphone.Country_Name__c=c1;
                    } 
            if(oldaccountphonelist!=null && oldaccountphonelist.size()>0){
              
                     system.debug('@@Preferred'+ (accphone.Preferred__c));
                        if(accphone.Preferred__c==true){                        
                            for(Account_Phone__c oldaccphone: oldaccountphonelist){
                                oldaccphone.Preferred__c=false;
                                oldaccountphoneupdatelist.add(oldaccphone);
                                accphoneinsertid.add(oldaccphone.Id);
                            }
                        }
                 
                 }else{
              
                          phone.Preferred__c=true;
                }
              
             }
                if(!oldaccountphoneupdatelist.IsEmpty()){
                          update oldaccountphoneupdatelist;
                      }
              
                  }
         }
    public void AccountPhoneUpdate(List<Account_Phone__c> newaccphonelist,List<Account_Phone__c> oldaccphonelist){
        Set<Id> phoneid=new Set<Id>();
        Set<Id> accid=new Set<Id>();
        
        List<Account_Phone__c> oldaccountphonelist=new List<Account_Phone__c>();
        List<Account_Phone__c> oldaccountphoneupdatelist=new List<Account_Phone__c>();
        
        if(newaccphonelist!=null&&newaccphonelist.size()>0){    
            for(Account_Phone__c accphoneval: newaccphonelist){
                phoneid.add(accphoneval.Id);
                accid.add(accphoneval.Account__c);
             }   
          
          oldaccountphonelist=[select Account__c,Preferred__c,Phone_Type__c from Account_Phone__c where Preferred__c=true AND Account__c IN:accid AND Id NOT IN:phoneid];
               
         for(Account_Phone__c newaccphone: newaccphonelist){  
         	 // To insert country code..
                    if(newaccphone.Country__c!=null){
                        String country=newaccphone.Country__c;
                        String c1=country.substring((country.indexOf('(')+1),country.indexOf(')'));
                        newaccphone.Country_Name__c=c1;
                    }           
                for(Account_Phone__c oldaccphone: oldaccphonelist){
                system.debug('##'+(newaccphone.Preferred__c));
                    if((newaccphone.Preferred__c==true &&  oldaccphone.Preferred__c==false&& newaccphone.Account__c==oldaccphone.Account__c)){ 
                        system.debug('INSIDEIF');
                        if(oldaccountphonelist!=null && oldaccountphonelist.size()>0){
                        for(Account_Phone__c accvalue: oldaccountphonelist){
                              accvalue.Preferred__c=false;        
                              oldaccountphoneupdatelist.add(accvalue);
                              accphoneupdateid.add(accvalue.Id);                            
                          }
                        } 
                    } else{
                    system.debug('INELSE');
                    system.debug('@@accphoneupdateid '+accphoneupdateid+'&&accphoneinsertid'+ accphoneinsertid);
                    for(Account_Phone__c accval: newaccphonelist){
                          
                            if(accphoneupdateid.contains(accval.id) ){
                            }else{
                            if(accphoneinsertid.IsEmpty()&&(newaccphone.Preferred__c==false && oldaccphone.Preferred__c==true))
                             trigger.new[0].addError('cannot update');  
                            }
                        }
                    
                    }               
                 }
                }    
                if(!oldaccountphoneupdatelist.isEmpty()){
                    update oldaccountphoneupdatelist;
                }                  
          }
    }
    public void AccountPhoneDelete(List<Account_Phone__c> accountphonelist){
        if(accountphonelist!=null&&accountphonelist.size()>0){
            system.debug('@@accountemaillist '+accountphonelist);
            for(Account_Phone__c accphone: accountphonelist){
            
            if(accphone.Preferred__c==true){
                    trigger.old[0].addError('Cannot delete prefered record');  
                }
            }
        
        }

    }
/************************************************************************
        Purpose     : Test Class                  
        Parameters  : None
        Returns     : None
***********************************************************************/
    @isTest(SeeAllData=false)
     static public void testAccountPhoneTriggerHandler() {
         PIMS_Reserved_Id__c pimsid=new PIMS_Reserved_Id__c();
         pimsid.PIMS_Id__c='0000-0000';
         pimsid.Used__c=false;
         insert pimsid;
         
         Account acc=new Account();
         acc.Name='test';
         acc.Type='SSO';
         insert acc;
     
         Account_Phone__c accphone=new Account_Phone__c();
         accphone.Account__c=acc.id;
         accphone.Country__c='Canada (CA)';
         accphone.Phone__c='12345';
         accphone.Phone_Type__c='Fax';
         accphone.Preferred__c=true;
         insert accphone;
         update accphone;
         
         Account_Phone__c accphone0=new Account_Phone__c();
         accphone0.Account__c=acc.id;
         accphone0.Country__c='Canada (CA)';
         accphone0.Phone__c='12345';
         accphone0.Phone_Type__c='Home';
         accphone0.Preferred__c=true;
         insert accphone0;
         
         Account_Phone__c accphone1=new Account_Phone__c();
         accphone1.Account__c=acc.id;
         accphone1.Country__c='Canada (CA)';
         accphone1.Phone__c='12345';
         accphone1.Phone_Type__c='Home';
         accphone1.Preferred__c=false;
         insert accphone1;
         accphone1.Preferred__c=true;
         update accphone1;
         
         Account_Phone__c accphone2=new Account_Phone__c();
         accphone2.Account__c=acc.id;
         accphone2.Country__c='Canada (CA)';
         accphone2.Phone__c='12345';
         accphone2.Phone_Type__c='Fax';
         accphone2.Preferred__c=false;
         insert accphone2;
         delete accphone2;
         
     }
}